package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.Button;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Demonstrates that the OrganizationsActionBean is working.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class TestOrganizationsActionBean {
  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  
  /** A string represents "MyISERN 1.3 - Add Organization". */
  private static final String ADD_PAGE = "MyISERN 1.3 - Add Organization";

  /**
   * Tests to view all organizations.
   * 
   * @throws Exception If error occur.
   */
  @Test
  public void testViewAllOrganizations() throws Exception {
    WebConversation conversation = new WebConversation();
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern-gold";
    WebResponse response = conversation.getResponse(url);

    // login the system
    WebForm loginForm = response.getFormWithID("loginForm");
    loginForm.setParameter("username", "isern");
    loginForm.setParameter("password", "password");
    response = loginForm.submit();

    // check if the user is login successfully by clicking the "View All Organizations" link again
    WebLink link = response.getLinkWith("View All Organizations");
    response = link.click();
    assertEquals("Clicking link after login", "MyISERN 1.3 - View All Organizations", response
        .getTitle());

    // check the organizations table
    WebTable organizationsTable = response.getTableWithID("organizationsTable");
    // check the first organizations in the table
    String name = organizationsTable.getCellAsText(1, 0);
    assertEquals("Checking organization table", "University of Maryland", name);

    // clicks on the first organizations to edit
    link = response.getLinkWith(name);
    response = link.click();
    // the page should become editOrganizations.jsp
    assertEquals("Clicking a organizations to edit", "MyISERN 1.3 - Edit Organization", response
        .getTitle());

    // checks if the current organizations being edited is correct
    WebForm editForm = response.getFormWithID("editSingleOrganizationForm");
    String contact = editForm.getParameterValue("organization.contact");
    assertEquals("Checking editing organizations type", "Victor Basili", contact);

    // changes the organization attribute and save it
    editForm.setParameter("organization.country", "Hawaii");
    Button button = editForm.getButtonWithID("saveButton");
    button.click();
    response = conversation.getCurrentPage();
    // the page should now go back to organizations.jsp
    assertEquals("Checking save after changing",
        "MyISERN 1.3 - View All Organizations", response.getTitle());

    // tries to add a new organization, but without any data
    link = response.getLinkWith("Add New Organization");
    response = link.click();
    // the page should become addOrganization.jsp
    assertEquals("Click to add a new organization",
        ADD_PAGE, response.getTitle());
    // leave everything blank by setting them to null
    WebForm addForm = response.getFormWithID("addSingleOrganizationForm");
    button = addForm.getButtonWithID("saveButton");
    button.click();
    response = conversation.getCurrentPage();
    // the page should be addCollaboration.jsp still, since the name is required
    assertEquals("Checking save after adding without a name",
        ADD_PAGE, response.getTitle());

    // tries to add another organization, but without affiliated researchers
    link = response.getLinkWith("Add New Organization");
    response = link.click();
    // the page should become addOrganization.jsp
    assertEquals("Click to add a new organization",
        ADD_PAGE, response.getTitle());
    // enter the name
    addForm.setParameter("organization.name", "New Organization");
    button.click();
    response = conversation.getCurrentPage();
    // the page should be organizations.jsp
    assertEquals("Checking save after adding",
        ADD_PAGE, response.getTitle());

    // tries to edit a collaboration via the link from sanity report
    link = response.getLinkWith("Sanity Report");
    response = link.click();
    // the page should become report.jsp
    assertEquals("Check the sanity report", "MyISERN 1.3 - Report", response.getTitle());

    // tries to click on the invalid collaboration link
    WebTable reportTable = response.getTableWithID("reportTable");
    int row = 0;
    // loop thru the table to find the link we want
    // only loops for 50 times to prevent infinite looping when the data got change
    while (row < 50) {
      if ("Some One in organization: FEI'S ORG".equals(reportTable.getCellAsText(row, 0))) {
        break;
      }
      row++;
      if (row == 50) {
        System.out.println("Data is modified, record cannot be found.");
      }
    }
    String linkString = reportTable.getCellAsText(row, 0);
    assertEquals("Checking the invalid link for collaboration",
        "Some One in organization: FEI'S ORG", linkString);
    link = response.getLinkWith(linkString);
    response = link.click();
    // the page should become editOrganization.jsp
    assertEquals("Clicking sanity report link to edit",
        "MyISERN 1.3 - Edit Organization", response.getTitle());
    // verifies the editing organization
    editForm = response.getFormWithID("editSingleOrganizationForm");
    name = editForm.getParameterValue("organization.name");
    assertEquals("Checking the editing orgazniation", "FEI'S ORG", name);
  }
}